package com.angcyo.bmob.api

import cn.bmob.data.Bmob
import cn.bmob.data.bean.op.BmobSms
import cn.bmob.data.bean.table.BmobUser
import cn.bmob.data.bean.type.BmobFile
import cn.bmob.data.callback.email.SendEmailListener
import cn.bmob.data.callback.file.DeleteFileListener
import cn.bmob.data.callback.file.UploadListener
import cn.bmob.data.callback.sms.SendSmsCodeListener
import cn.bmob.data.callback.sms.VerifySmsCodeListener
import cn.bmob.data.callback.user.ResetPasswordListener
import cn.bmob.data.config.BmobConfig
import cn.bmob.data.exception.BmobException
import com.angcyo.log.L
import java.io.File

/**
 * Bmob的异步回调, 都在子线程处理. 请注意更新UI操作.
 *
 * Email:angcyo@126.com
 * @author angcyo
 * @date 2021/01/19
 */
object BmobApi {
    fun init(appId: String, apiKey: String, masterKey: String? = null, debug: Boolean = L.debug) {
        Bmob.getInstance().init(appId, apiKey, masterKey)

        BmobConfig.setDebug(debug)
        BmobConfig.setSynchronous(false)
    }
}

fun isBmobLogin() = BmobUser.getInstance().isLogin

inline fun <reified T : BmobUser> getBmobCurrentUser(): T? = BmobUser.getInstance().getCurrentUser(T::class.java)

/**验证短信验证码*/
fun bmobVerifySmsCode(phoneNumber: String, smsCode: String, verifySmsCodeListener: VerifySmsCodeListener) {
    val bmobSms = BmobSms()
    bmobSms.mobilePhoneNumber = phoneNumber
    bmobSms.verifySmsCode(smsCode, verifySmsCodeListener)
}

/**发送验证短信，如果使用默认模板，则设置template为空字符串或不设置*/
fun bmobSendSms(phoneNumber: String, template: String, sendSmsCodeListener: SendSmsCodeListener) {
    val bmobSms = BmobSms()
    bmobSms.mobilePhoneNumber = phoneNumber
    bmobSms.template = template
    bmobSms.sendSmsCode(sendSmsCodeListener)
}

/**发送验证用户邮箱的邮件*/
fun bmobSendEmailForVerifyUserEmail(email: String, end: (msg: String?, ex: BmobException?) -> Unit) {
    BmobUser.sendEmailForVerifyUserEmail(email, object : SendEmailListener() {
        override fun onFailure(ex: BmobException?) {
            end(null, ex)
        }

        override fun onSuccess(msg: String?) {
            end(msg, null)
        }
    })
}

/**发送重置密码的邮件*/
fun bmobSendEmailForResetPassword(email: String, end: (msg: String?, ex: BmobException?) -> Unit) {
    BmobUser.sendEmailForResetPassword(email, object : SendEmailListener() {
        override fun onFailure(ex: BmobException?) {
            end(null, ex)
        }

        override fun onSuccess(msg: String?) {
            end(msg, null)
        }
    })
}

/**使用短信验证码重置密码*/
fun bmobResetPasswordBySmsCode(smsCode: String, newPassword: String, end: (msg: String?, ex: BmobException?) -> Unit) {
    BmobUser.resetPasswordBySmsCode(smsCode, newPassword, object : ResetPasswordListener() {
        override fun onFailure(ex: BmobException?) {
            end(null, ex)
        }

        override fun onSuccess(msg: String?) {
            end(msg, null)
        }
    })
}

/**使用旧密码重置密码，需要用户登录*/
inline fun <reified T : BmobUser> bmobResetPasswordByOldPassword(
    oldPassword: String,
    newPassword: String,
    crossinline end: (msg: String?, ex: BmobException?) -> Unit
) {
    if (!isBmobLogin()) {
        end(null, BmobException("请先登录", 500))
        return
    }
    getBmobCurrentUser<T>()?.resetPasswordByOldPassword(oldPassword, newPassword, object : ResetPasswordListener() {
        override fun onFailure(ex: BmobException?) {
            end(null, ex)
        }

        override fun onSuccess(msg: String?) {
            end(msg, null)
        }
    })
}

/**上传文件*/
fun bmobUploadFile(file: File, uploadListener: UploadListener) {
    val bmobFile = BmobFile(file)
    bmobFile.uploadFile(uploadListener)
}

fun bmobDeleteFile(file: File, deleteFileListener: DeleteFileListener) {
    val bmobFile = BmobFile(file)
    bmobFile.deleteFile(deleteFileListener)
}